package simulation;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class FlattenNestedListIterator341 implements Iterator<Integer> {
    LinkedList<Integer> stack = new LinkedList<>();

    private void push(List<NestedInteger> nestedList){
        for (int i = nestedList.size() - 1; i >= 0; i--) {
            NestedInteger nestedInteger = nestedList.get(i);
            if(!nestedInteger.isInteger()){
                push(nestedInteger.getList());
            }else {
                stack.push(nestedInteger.getInteger());

            }
        }
    }

    public FlattenNestedListIterator341(List<NestedInteger> nestedList) {
        push(nestedList);
    }

    @Override
    public Integer next() {
        return stack.pop();
    }

    @Override
    public boolean hasNext() {
        return stack.size() == 0? false:true;
    }

    public interface NestedInteger {

        // @return true if this NestedInteger holds a single integer, rather than a nested list.
        public boolean isInteger();

        // @return the single integer that this NestedInteger holds, if it holds a single integer
        // Return null if this NestedInteger holds a nested list
        public Integer getInteger();

        // @return the nested list that this NestedInteger holds, if it holds a nested list
        // Return empty list if this NestedInteger holds a single integer
        public List<NestedInteger> getList();
    }

}
